package org.spring.springboot.exception.service.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spring.springboot.exception.common.ascept.Result;
import org.spring.springboot.exception.common.ascept.ResultUtil;
import org.spring.springboot.exception.common.selfex.BusinessException;
import org.spring.springboot.exception.common.selfex.ExceptionType;
import org.spring.springboot.exception.dao.CityDao;
import org.spring.springboot.exception.domain.City;
import org.spring.springboot.exception.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 城市业务逻辑实现类
 *
 * Created by bysocket on 07/02/2017.
 */
@Service
public class CityServiceImpl implements CityService {
	
	private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private CityDao cityDao;

	public Result<City> findCityByName(String cityName) throws BusinessException {
    
    	logger.info("------------>" + cityName);
    	//cityName.substring(12);
    	City city1 = cityDao.findByName(cityName);
    	
    	findCitys();
    	
    	City city = new City(); 
		city.setCityName("湖州1");
		city.setDescription("浙江1");
		city.setProvinceId(16L);
		int i = cityDao.addCity(city);

        return ResultUtil.success(city);
    }

	@Override
	@Transactional(rollbackFor=Exception.class,propagation=Propagation.REQUIRED)
	public Result<Map<String, String>> findCitys() throws BusinessException {
		List<City> citys = cityDao.findCitys();
		
		City city = new City(); 
		city.setCityName("湖州");
		city.setDescription("浙江");
		city.setProvinceId(16L);
		int i = cityDao.addCity(city);
		
		logger.info("返回编号：" + city.getId());
		try {
			city.getCityName().substring(12);
		} catch (Exception e) {			
			throw new BusinessException(ExceptionType.DATA_ERROR);
		}
		logger.info("citys.length=" + i);
		return ResultUtil.success();
	}

}
